﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using MM.DBUtility;

namespace JobReadTxt
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string strFilePath = textBox1.Text.Trim();
                if (!string.IsNullOrEmpty(strFilePath))
                {
                    StringBuilder sbSQL = new StringBuilder();
                    foreach (string strLine in File.ReadAllLines(strFilePath, Encoding.GetEncoding("GB2312")))
                    {
                        //移动段号读取方法
                        //if (!string.IsNullOrEmpty(strLine) && strLine.Contains(","))
                        //{
                        //    string[] strArg = strLine.Split(',');
                        //    if (strArg.Length > 2)
                        //    {
                        //        string str1 = strArg[0];
                        //        string str2 = strArg[1];
                        //        string str3 = strArg[2];

                        //        string strSQL = string.Format("INSERT INTO T_DH(ProvinceName,CityName,DH) VALUES('{0}','{1}','{2}')", str1, str2, str3);
                        //        MM.DBUtility.DbHelperSql.ExecuteSql(strSQL);
                        //    }
                        //}

                        //联通段号读取方法
                        //if (!string.IsNullOrEmpty(strLine) && strLine.Length > 30)
                        //{
                        //    string[] strArg = strLine.Split(' ');
                        //    if (strArg.Length == 22)
                        //    {
                        //        string strDH = strArg[0];
                        //        string strPN = strArg[1];
                        //        string strCN = strArg[8];
                        //        string strFlag = strArg[21];

                        //        if (!strFlag.Equals("1"))
                        //        {
                        //            string strSQL = string.Format("INSERT INTO T_DH1(ProvinceName,CityName,DH) VALUES('{0}','{1}','{2}')", strPN, strCN, strDH);
                        //            MM.DBUtility.DbHelperSql.ExecuteSql(strSQL);
                        //        }
                        //    }
                        //}
                        //linkid读取
                        if (!string.IsNullOrEmpty(strLine))
                        {
                            sbSQL.Append(string.Format(" SELECT  '{0}' linkid UNION ", strLine));
                        }
                    }
                    string strSQL = sbSQL.ToString();
                    strSQL = "INSERT INTO T_Temp(linkid) " + strSQL.Remove(strSQL.LastIndexOf("UNION"));
                    MM.DBUtility.DbHelperSql.ExecuteSql(strSQL);

                    MessageBox.Show("生成成功！");
                }
                else
                {
                    MessageBox.Show("请选择文件！");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.InitialDirectory = "c:\\";//注意这里写路径时要用c:\\而不是c:\
                openFileDialog.Filter = "文本文件|*.*|C#文件|*.cs|所有文件|*.*";
                openFileDialog.RestoreDirectory = true;
                openFileDialog.FilterIndex = 1;
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    textBox1.Text = openFileDialog.FileName;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
